package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.*;
import com.zero.clinic.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/patientRegister")
public class PatientRegisterController {
    @Resource
    private PatientRegisterService patientRegisterService;
    @Resource
    private DeptService deptService;
    @Resource
    private RegisterService registerService;
    @Resource
    private UserService userService;

    /**
     * @function 进入门诊挂号管理模块
     * @return
     */
    @RequiresPermissions("patientRegister:patientRegisterPage")
    @RequestMapping("/patientRegisterPage")
    public String patientRegisterPage(){
        return "patientRegisterPage";
    }

    /**
     * @function 门诊挂号列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("patientRegister:list")
    @RequestMapping("/list")
    public PageInfo<PatientRegister> patientRegisterList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的PatientRegister信息
        PatientRegisterExample example = new PatientRegisterExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据根患者姓名查询
            PatientRegisterExample.Criteria criteria1 = example.createCriteria();
            criteria1.andPatientNameLike("%"+ keywords +"%");
            // 2.2 根据根患者住址查询
            PatientRegisterExample.Criteria criteria2 = example.or();
            criteria2.andPatientAddressLike("%"+ keywords +"%");
            // 2.3 根据挂号科室查询
            PatientRegisterExample.Criteria criteria3 = example.or();
            criteria3.andDeptNameLike("%"+ keywords +"%");
            // 2.4 根据挂号医生查询
            PatientRegisterExample.Criteria criteria4 = example.or();
            criteria4.andDoctorNameLike("%"+ keywords +"%");
            // 2.5 根据挂号类型查询
            PatientRegisterExample.Criteria criteria5 = example.or();
            criteria5.andRegisterNameLike("%"+ keywords +"%");
            // 2.6 根据挂号员姓名查询
            PatientRegisterExample.Criteria criteria6 = example.or();
            criteria6.andRegisterOperatorNameLike("%"+ keywords +"%");
        }
        List<PatientRegister> patientRegisters = patientRegisterService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<PatientRegister> pageInfo = new PageInfo<>(patientRegisters);
        return pageInfo;
    }

    /**
     * @function 编辑门诊挂号页面
     * @param patientRegisterId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String patientRegisterEdit(Long patientRegisterId, Model m){
        // 1.如果前端页面需要更新门诊挂号信息，则会传一个需要更新的patientRegisterId过来。这里需要通过patientRegisterId查出更新信息后共享给前端页面
        if (patientRegisterId != null) {
            PatientRegister patientRegister = patientRegisterService.selectByPrimaryKey(patientRegisterId);
            Date patientBirthday = patientRegister.getPatientBirthday();
            patientRegister.setPatientBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(patientBirthday));
            Date registerTime = patientRegister.getRegisterTime();
            patientRegister.setRegisterTimeStr(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(registerTime));
            System.out.println("当前操作的门诊挂号信息：" + patientRegister);
            m.addAttribute("patientRegister",patientRegister);
        }

        // 2.将可供选择的挂号科室共享给前端页面
        DeptExample deptExample = new DeptExample();
        DeptExample.Criteria criteria1 = deptExample.createCriteria();
        criteria1.andIsRegisterDeptEqualTo(1L);
        List<Dept> registerDepts = deptService.selectByExample(deptExample);
        m.addAttribute("registerDepts", registerDepts);

        // 3.将可供选择的挂号类型信息共享给前端页面
        RegisterExample registerExample = new RegisterExample();
        List<Register> registers = registerService.selectByExample(registerExample);
        m.addAttribute("registers", registers);

        // 4.将可供选择的挂号员信息共享给前端页面
        UserExample userExample2 = new UserExample();
        UserExample.Criteria criteria2 = userExample2.createCriteria();
        criteria2.andJobNameEqualTo(Constant.JOB_NAME_REGISTER_OPERATOR);
        List<User> registerOperators = userService.selectByExample(userExample2);
        m.addAttribute("registerOperators", registerOperators);

        // 5.在更新门诊挂号信息的情况下，提供当前科室的医生信息共享给前端页面
        // 5.1 如果前端页面传过来的patientRegisterId不为空，就查询到当前需要更新的PatientRegister
        if (patientRegisterId != null){
            PatientRegister patientRegister = patientRegisterService.selectByPrimaryKey(patientRegisterId);

            // 5.2 先定义出查询医生的条件
            UserExample userExample3 = new UserExample();
            UserExample.Criteria criteria3 = userExample3.createCriteria();
            criteria3.andJobNameEqualTo(Constant.JOB_NAME_DOCTOR);
            // 5.3 再根据patientRegister的registerDept属性定义出科室的条件
            criteria3.andDeptIdEqualTo(patientRegister.getRegisterDept());
            List<User> currentDeptDoctors = userService.selectByExample(userExample3);
            m.addAttribute("currentDeptDoctors", currentDeptDoctors);
        }

        return "patientRegisterEdit";
    }

    /**
     * @function 新增门诊挂号
     * @param patientRegister
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("patientRegister:insert")
    public MessageObject patientRegisterInsert(@RequestBody PatientRegister patientRegister){
        //1.将当前时间作为门诊挂号时间，赋值给新增的patientRegister对象
        patientRegister.setRegisterTime(new Date());
        System.out.println("需要新增的门诊挂号：" + patientRegister);
        //2.插入数据
        int row = patientRegisterService.insert(patientRegister);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新门诊挂号
     * @param patientRegister
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("patientRegister:update")
    public MessageObject patientRegisterUpdate(@RequestBody PatientRegister patientRegister){
        //1.更新数据
        System.out.println("需要更新的门诊挂号：" + patientRegister);
        int row = patientRegisterService.updateByPrimaryKeySelective(patientRegister);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 显示门诊挂号详情
     * @param patientRegisterId
     * @param m
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("patientRegister:detail")
    public String patientRegisterDetail(Long patientRegisterId, Model m){
        // 1.如果前端页面需要显示门诊挂号详情，则会传一个需要显示的patientRegisterId过来。这里需要通过patientRegisterId查出详细信息后共享给前端页面
        if (patientRegisterId != null) {
            PatientRegister patientRegister = patientRegisterService.selectByPrimaryKey(patientRegisterId);
            Date patientBirthday = patientRegister.getPatientBirthday();
            patientRegister.setPatientBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(patientBirthday));
            Date registerTime = patientRegister.getRegisterTime();
            patientRegister.setRegisterTimeStr(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(registerTime));
            m.addAttribute("patientRegister",patientRegister);
        }

        // 2.将可供显示的挂号科室共享给前端页面
        DeptExample deptExample = new DeptExample();
        DeptExample.Criteria criteria1 = deptExample.createCriteria();
        criteria1.andIsRegisterDeptEqualTo(1L);
        List<Dept> registerDepts = deptService.selectByExample(deptExample);
        m.addAttribute("registerDepts", registerDepts);

        // 3.将可供显示的挂号类型信息共享给前端页面
        RegisterExample registerExample = new RegisterExample();
        List<Register> registers = registerService.selectByExample(registerExample);
        m.addAttribute("registers", registers);

        // 4.将可供显示的挂号员信息共享给前端页面
        UserExample userExample2 = new UserExample();
        UserExample.Criteria criteria2 = userExample2.createCriteria();
        criteria2.andJobNameEqualTo(Constant.JOB_NAME_REGISTER_OPERATOR);
        List<User> registerOperators = userService.selectByExample(userExample2);
        m.addAttribute("registerOperators", registerOperators);

        // 5.将可供显示的所有医生信息共享给前端页面
        UserExample userExample3 = new UserExample();
        UserExample.Criteria criteria3 = userExample3.createCriteria();
        criteria3.andJobNameEqualTo(Constant.JOB_NAME_DOCTOR);
        List<User> allDoctors = userService.selectByExample(userExample3);
        m.addAttribute("allDoctors", allDoctors);

        return "patientRegisterDetail";
    }

    /**
     * @function 删除门诊挂号
     * @param patientRegisterId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("patientRegister:delete")
    public MessageObject patientRegisterDelete(Long patientRegisterId){
        //1.根据patientRegisterId删除数据
        int row = patientRegisterService.deleteByPrimaryKey(patientRegisterId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除门诊挂号
     * @param patientRegisterIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("patientRegister:batchdel")
    public MessageObject patientRegisterBatchDelete(String patientRegisterIds){
        String[] patientRegisterIdsArr = patientRegisterIds.split(",");
        //2.根据patientRegisterIdsArr删除数据
        int row = patientRegisterService.deleteByIds(patientRegisterIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

    /**
     * @function 通过前端选中的门诊挂号ID查询对应门诊挂号数据并返回
     * @param patientRegisterId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getPatientRegisterInfoById")
    public PatientRegister getPatientRegisterInfoById(Long patientRegisterId){
        PatientRegister patientRegister = patientRegisterService.selectByPrimaryKey(patientRegisterId);
        Date patientBirthday = patientRegister.getPatientBirthday();
        patientRegister.setPatientBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(patientBirthday));
        Date registerTime = patientRegister.getRegisterTime();
        patientRegister.setRegisterTimeStr(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(registerTime));
        return  patientRegister;
    }
}
